//
// Created by haoy on 2017/3/13.
//

#ifndef RECORD_RTP_INFO_READER_HPP
#define RECORD_RTP_INFO_READER_HPP

#include <algorithm>
#include <stdint.h>
#include <map>

#include "dbg.hpp"
#include "mod/rtp.h"
#include "mod/video.h"
#include "mod/vp8/vp8.h"
#include "media_hapi.h"
#include "packet.hpp"
#include "packet_reorder.hpp"
#include "tlv.hpp"
#include "util.h"
#include "vpx/vpx_codec.h"
#include "vpx/vpx_decoder.h"
#include "vpx/vp8.h"
#include "vpx/vp8dx.h"



class RTPPreProcess : public PacketHandler {
public:
    RTPPreProcess();
    ~RTPPreProcess();

    bool hasVideo();
    bool hasAudio();
    mediacodec_id_t getAudioCodec();
    mediacodec_id_t getVideoCodec();
    uint32_t getVideoWidth();
    uint32_t getVideoHeight();
    uint32_t getAudioSampleRate();
    uint32_t getAudioChannels();

private:
    void onCodecRegistry(CodecRegistry& codecs, uint32_t pt, const std::string& codec_name) override;

    void onAudioPacket(RTPPacket* packet, uint64_t pts) override;

    void onVideoPacket(RTPPacket* packet, uint64_t pts) override;

private:
    RTPDepacketizer* _video_depacketizer = NULL;
    CodecRegistry* _codecs;

    bool _has_video = false;
    mediacodec_id_t _video_codec = MCODEC_ID_UNKNOWN;
    uint32_t _video_width = 0;
    uint32_t _video_height = 0;
    uint32_t _video_fps = 0;

    bool _has_audio = false;
    mediacodec_id_t _audio_codec = MCODEC_ID_UNKNOWN;
    uint32_t _audio_sample_rate = 0;
    uint32_t _audio_channels = 0;

    vpx_codec_ctx_t _vpx_codec_ctx;
    bool _vpx_codec_inited = false;

    uint32_t _next_video_extseq = (uint32_t)-1;
    // currently, only h.264 use this decoder
    VideoDecoder* _video_decoder = nullptr;
};

#endif //RECORD_RTP_INFO_READER_HPP
